package com.laowu.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.laowu.bean.ResponseMsg;
import com.laowu.bean.User;
import com.laowu.bean.mapper.UserDao;
import com.laowu.constants.CodeConstants;

@Controller
@CrossOrigin  // 用于跨域请求
public class LoginController {

	@Resource
	UserDao userDao;

	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = {})
	public @ResponseBody ResponseMsg login(User user) {
		System.out.println("user ----> " + user);
		String paramName = "";
		if (user != null && user.getUserName() != null && user.getUserName() != "") {
			paramName = user.getUserName();
		} else {
			return new ResponseMsg(CodeConstants.USERNAME_IS_EMPTY, "请输入用户名！");
		}
		if (user == null || user.getUserPassword() == null || user.getUserPassword() == "") {
			return new ResponseMsg(CodeConstants.PASSWORD_IS_EMPTY, "请输入密码！");
		}
		List<User> allUsers = userDao.getAllUsers(paramName);
		// 登录操作判断
		if (allUsers.size() > 0) {
			User getUser = allUsers.get(0);
			if (getUser.getUserPassword().equals(user.getUserPassword())) {
				return new ResponseMsg(CodeConstants.SUCCESSFUL, getUser);
			} else {
				return new ResponseMsg(CodeConstants.PASSWORD_ERROR, "请输入正确的密码！");
			}
		} else {
			return new ResponseMsg(CodeConstants.USER_IS_NOT, "未查询到用户信息！");
		}

	}

	@RequestMapping(value = "/getAllUsers", method = RequestMethod.GET, produces = {})
	public @ResponseBody ResponseMsg users() {
		ResponseMsg responseMsg = new ResponseMsg(CodeConstants.SUCCESSFUL, userDao.getAllUsers(""));
		return responseMsg;
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = {})
	@ResponseBody
	public ResponseMsg addUser(@RequestBody User user) {
		userDao.addUser(user);
		return new ResponseMsg(CodeConstants.SUCCESSFUL, user);
	}

	@RequestMapping(value = "/getAllUserAndRole", method = RequestMethod.GET)
	public @ResponseBody ResponseMsg getAllUserAndRole(@Param("name") String name) {
		String paramName = "";
		List<User> allUserAndRole = null;
		System.out.println("name:" + name);
		if (name != null && name != "") {
			paramName = name;
		}
		allUserAndRole = userDao.getAllUserAndRole(paramName);
		return new ResponseMsg(CodeConstants.SUCCESSFUL, allUserAndRole);
	}

	@RequestMapping(value = "/postTest", method = RequestMethod.POST, produces = {})
	@ResponseBody
	public ResponseMsg postTest(String userName, MultipartFile image) {
		System.out.println("post test userName ---> " + userName);
		System.out.println("post test image ---> " + image.getName());
		return new ResponseMsg(CodeConstants.SUCCESSFUL, userName);
	}
}
